package cn.iocoder.yudao.module.ent.controller.open.search;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.ent.controller.admin.company.vo.*;
import cn.iocoder.yudao.module.ent.convert.company.CompanyConverter;
import cn.iocoder.yudao.module.ent.document.EntCompany;
import cn.iocoder.yudao.module.ent.enums.EntSearchReportEnum;
import cn.iocoder.yudao.module.ent.service.company.CompanyService;
import cn.iocoder.yudao.module.ent.service.search.SearchService;
import cn.iocoder.yudao.module.ent.service.searchreport.SearchReportService;
import cn.iocoder.yudao.module.system.api.tenant.TenantApi;
import cn.iocoder.yudao.module.system.api.tenant.dto.TenantDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "企业搜索")
@RestController
@RequestMapping("/ent/search")
@Validated
@Slf4j
public class OpenSearchController {
    @Resource
    private SearchService searchService;

    @Resource
    private CompanyService companyService;

    @Resource
    private SearchReportService searchReportService;

    @Resource
    private TenantApi tenantApi;

    @GetMapping("/get-list")
    @Operation(summary = "普通查询")
    public CommonResult<QueryRespVO> getList(@Valid @RequestBody QueryVO queryVO) {
        System.out.println(queryVO);
        QueryRespVO queryRespVO = searchService.queryList(queryVO);
        return success(queryRespVO);
    }

    @GetMapping("/get-area")
    @OperateLog(enable = false)
    public CommonResult<Integer> getArea(@RequestParam("address") String address) {
        return success(AreaUtils.parseAddress(address));
    }

    @GetMapping("/get-one")
    @Parameter(name = "cid", description = "主键", required = true, example = "1024")
    @Operation(summary = "单条查询")
    @OperateLog(enable = false)
    public CommonResult<BaseDataVO> getOne(@RequestParam("cid") String cid) {
        log.info("tenant_id:{}", TenantContextHolder.getTenantId());
        ArrayList<Long> tenantIdList = new ArrayList<Long>(){{
            add(TenantContextHolder.getTenantId());
        }};
        Map<Long, TenantDTO> tenantMap = tenantApi.getTenantMap(tenantIdList);
        EntCompany entCompany = searchService.queryById(cid);
        BaseDataVO baseDataVO;
        if(tenantMap.isEmpty() || tenantMap.get( TenantContextHolder.getTenantId()).getAuthType() == 2){
            //个人版不查详情
            baseDataVO = new BaseDataVO();
            baseDataVO.setCompanyInfo(BeanUtils.toBean(entCompany, EntCompanyVO.class));
            baseDataVO.setMenuList(new ArrayList<>());
            baseDataVO.setNumberList(new ArrayList<>());
        }else{
            baseDataVO = companyService.convertDataVO(entCompany);
            @Valid ReportReqVO reportReqVO = new ReportReqVO().setData(cid).setType(EntSearchReportEnum.DETAIL.getType());
            searchReportService.createReport(reportReqVO);
        }
        return success(baseDataVO);
    }


    @PostMapping("/get-latest")
    @Operation(summary = "获取近30天新增的企业")
    @OperateLog(enable = false)
    public CommonResult<QueryRespVO<BaseDataVO>> getLatest(@Valid @RequestBody QueryVO queryVO) {
        QueryRespVO<EntCompany> queryRespVO = searchService.queryLatest(queryVO);
        QueryRespVO<BaseDataVO> baseDataVOQueryRespVO = CompanyConverter.INSTANCE.convert0(queryRespVO);
        return success(baseDataVOQueryRespVO);
//        return success(queryRespVO);
    }

    @PostMapping("/get-status")
    @OperateLog(enable = false)
    @Operation(summary = "查询领取信息和联系方式等信息")
    public CommonResult<Map<String, BaseDataVO>> getStatus(@Valid @RequestBody BaseReqVO baseReqVO) {
        Map<String, BaseDataVO> result = companyService.queryStatus(baseReqVO);
        return success(result);
    }
}

